package com.mhxy.controller;

import com.mhxy.constant.TaskConstants;
import com.mhxy.service.TaskInterface;
import com.mhxy.handler.TaskProgressHandler;
import com.mhxy.service.impl.*;
import com.mhxy.service.impl.InstanceNormalTaskImpl;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TaskController {
    private final String hwndName;

    public TaskController(WinDef.HWND hwnd_0, String hwndName) throws Exception {
        this.hwndName = hwndName;
        executeTaskQueue(hwnd_0);
    }

    private void executeTaskQueue(WinDef.HWND hwnd_0) throws Exception {
        while (true) {
            String taskName = TaskProgressHandler.poolTask(hwndName);
            if (taskName == null) {
                break;
            }

            TaskInterface taskInterface = getMyTask(hwnd_0, taskName, hwndName);
            if (taskInterface == null) {
                log.error("任务 {} 实现类不存在", taskName);
                continue;
            }

            executeTask(taskInterface);

        }
    }

    private void executeTask(TaskInterface taskInterface) {
        String taskName = null;
        try {
            taskName = taskInterface.getTaskName();
            log.info("开始执行任务: {}", taskName);
            
            // 任务准备阶段
            TaskProgressHandler.updateProgress(taskName, "0%");
            if (!executeTaskPhase(taskInterface::before, "准备")) {
                return;
            }

            // 任务执行阶段
            TaskProgressHandler.updateProgress(taskName, "30%");
            if (!executeTaskPhase(taskInterface::execute, "执行")) {
                return;
            }

            // 任务完成阶段
            TaskProgressHandler.updateProgress(taskName, "60%");
            if (!executeTaskPhase(taskInterface::after, "完成")) {
                return;
            }

            // 任务成功完成
            TaskProgressHandler.updateProgress(taskName, "100%");
            log.info("任务执行成功: {}", taskName);
            return;
        } catch (Exception e) {
            String errorMsg = String.format("任务执行异常: %s", e.getMessage());
            log.error(errorMsg, e);
            if (taskName != null) {
                TaskProgressHandler.updateProgress(taskName, "错误");
            }
        }
    }

    private boolean executeTaskPhase(TaskPhase phase, String phaseName) throws Exception {
        Boolean result = phase.execute();
        if (result == null) {
            return false;
        }
        if (!result) {
            log.warn("任务{}阶段失败", phaseName);
            return false;
        }
        return true;
    }

    @FunctionalInterface
    private interface TaskPhase {
        Boolean execute() throws Exception;
    }

    private TaskInterface getMyTask(WinDef.HWND hwnd_0, String task, String hwndName) throws Exception {
        switch (task) {
            case TaskConstants.LOGIN_GAME:  // 登录游戏
                return new LoginGameTaskImpl(hwnd_0, hwndName);
            case TaskConstants.SWITCH_ACCOUNT:  // 切换登录账号
                return new SwitchAccountTaskImpl(hwnd_0, hwndName);
            /**
             * 日常单人任务
             */
            case TaskConstants.MASTER_SCHOOL:  // 自动师门
                return new MasterSchoolImpl(hwnd_0);
            case TaskConstants.SCRATCH_TICKET:  // 刮刮乐
                return new ScratchTicketTaskImpl(hwnd_0);
            case TaskConstants.MAP_TASK:    // 藏宝图
                return new MapGetTaskImpl(hwnd_0);
            case TaskConstants.USE_MAP:     // 使用宝图
                return new MapUseTaskImpl(hwnd_0);
            case TaskConstants.DART_CAR:    // 押普通镖
                return new DartCarTaskImpl(hwnd_0);
            case TaskConstants.AQ_TASK:     // 三界奇缘
                return new AQTaskmpl(hwnd_0);
            case TaskConstants.SELL_SILVER_COIN:    // 物品卖银币
                return new SellSilverCoinTaskImpl(hwnd_0);
            case TaskConstants.RECEIVE_POINT:   // 领取双倍
                return new ReceivePointTaskImpl(hwnd_0);
            case TaskConstants.SECRET_AREA_20:     // 秘境降妖(20)
                return new SecretAreaTaskImpl(hwnd_0, 20, hwndName);
            case TaskConstants.SECRET_AREA_25:     // 秘境降妖(25)
                return new SecretAreaTaskImpl(hwnd_0, 25, hwndName);
            case TaskConstants.EXAM_TASK:       // 科举考试
                return new ExamTaskImpl(hwnd_0);
            case TaskConstants.USE_ENERGY:    // 使用活力
                return new UseEnergyTaskImpl(hwnd_0);
            case TaskConstants.SELL_TEMP_SYMBOL:    // 出售临时符
                return new SellTempSymbolTaskImpl(hwnd_0);
            case TaskConstants.GO_HOME_SLEEP:    // 回家睡觉
                return new GoHomeSleepTaskImpl(hwnd_0);
            case TaskConstants.FACTION_TASK:    // 帮派任务
                return new FactionTaskImpl(hwnd_0, hwndName);
            /**
             * 日常多人任务
             */
            case TaskConstants.INSTANCE_NORMAL1:     // 普通副本1
                return new InstanceNormalTaskImpl(hwnd_0, 1);
            case TaskConstants.INSTANCE_NORMAL2:     // 普通副本2
                return new InstanceNormalTaskImpl(hwnd_0, 2);
            case TaskConstants.INSTANCE_NORMAL3:     // 普通副本3
                return new InstanceNormalTaskImpl(hwnd_0, 3);
            case TaskConstants.INSTANCE_HARD_1:     // 侠士副本1
                return new InstanceHardTaskImpl(hwnd_0, 1);
            case TaskConstants.INSTANCE_HARD_2:     // 侠士副本2
                return new InstanceHardTaskImpl(hwnd_0, 2);
            case TaskConstants.FIGHT_GHOST:     //  抓鬼
                return new FightGhostImpl(hwnd_0, hwndName);
            case TaskConstants.JIAN_HUI:     //  剑会
                return new JianHuiTaskImpl(hwnd_0);
            case TaskConstants.LEI_TAI:     //  擂台大挑战
                return new LeiTaiTaskImpl(hwnd_0);
            /**
             * 活动任务
             */
            case TaskConstants.PET_PART:        // 召唤灵乐园
                return new PetPartTaskImpl(hwnd_0);
            case TaskConstants.CLIMB_TOWER:     // 勇闯迷魂塔
                return new ClimbTowerTaskImpl(hwnd_0);
            case TaskConstants.CLEAR_STAGE:     // 门派闯关
                return new ClearStagesTaskImp(hwnd_0);
            default:
                return null;
        }
    }
}
